package com.lzyyj.gmis.base.model;

import java.io.File;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;

/**
 * Web主题按布局分为若干类型，每种类型一个文件夹；每种类型又分为若干风格，
 * 每种风格一个文件；每个风格目录下至少一个风格文件，其余为图标或图片文件
 * @author lzyyj
 *
 */
public class ThemeSkin {

	private static Log log = LogFactory.getLog(ThemeSkin.class);
	
	public static final String THEME_ROOT_KEY = "themeRoot";
	
	public static final String THEME_TYPE_KEY = "themeType";

	public static final String THEME_STYLE_KEY = "themeStyle";

	public static final String THEME_MAIN_CSS_KEY = "mainCSS";

	private static String[] excludeFolders = new String[]{"WEB-INF","META-INF","jquery"};
	
	/**
	 * url 路径
	 */
	private String skinRoot;
	
	private Map<String, ThemeType> themeTypeMap = new HashMap<String, ThemeType>();
	
	private String defaultType = null;

	private ThemeSkin(String path) {
		this.skinRoot = path;
	}

	public static ThemeSkin getInstance(String basePath, String skinRoot, String defaultThemeType, String defaultThemeStyle) {
		ThemeSkin themeSkin = null;
		
		String path = basePath;
		if (skinRoot==null)skinRoot="";
		
		if (skinRoot.length()>0){
			if (!path.endsWith(File.separator)){
				path += File.separator;
			}
			path += skinRoot;
		}
		
		File file =new File(path);
		if (!file.exists()||!file.isDirectory()){
			// 指定Skin目录无效，自动搜索样式文件，构建Skin
			file = new File(basePath);
			for (File subFolder: file.listFiles()){
				if (subFolder.isDirectory()){
					boolean exclude = false;
					for(String excludeFolder:excludeFolders){
						if(subFolder.getName().equalsIgnoreCase(excludeFolder)){
							exclude = true;
							break;
						}
					}
					if(!exclude){
						themeSkin = new ThemeSkin(subFolder.getName());
						
						for (File themeTypeFolder: subFolder.listFiles()){
							if (themeTypeFolder.isDirectory()){
								ThemeType themeType = ThemeType.getInstance(themeSkin, themeTypeFolder, defaultThemeStyle);
								if (themeType!=null){
									themeSkin.themeTypeMap.put(themeTypeFolder.getName().toLowerCase(),themeType);
									if (themeSkin.defaultType==null||themeTypeFolder.getName().equalsIgnoreCase(defaultThemeType)){
										themeSkin.defaultType = themeTypeFolder.getName().toLowerCase();
									}
								}
							}
						}
						if(themeSkin.themeTypeMap.size()==0){
							themeSkin = null;
							break;
						}
					}
				}
			}

			if (file.exists()&&file.isDirectory()){
				boolean exclude = false;
				for(String excludeFolder:excludeFolders){
					if(file.getName().equalsIgnoreCase(excludeFolder)){
						exclude = true;
						break;
					}
				}
				if(!exclude){
					// 存在而且是文件夹，尝试构建主题类型对象
					themeSkin = new ThemeSkin(skinRoot);
					for (File themeTypeFile: file.listFiles()){
						if (themeTypeFile.isDirectory()){
							ThemeType themeType = ThemeType.getInstance(themeSkin, themeTypeFile, defaultThemeStyle);
							if (themeType!=null){
								themeSkin.themeTypeMap.put(themeTypeFile.getName().toLowerCase(),themeType);
								if (themeSkin.defaultType==null||themeTypeFile.getName().equalsIgnoreCase(defaultThemeType)){
									themeSkin.defaultType = themeTypeFile.getName().toLowerCase();
								}
							}
						}
					}
				}
			}
		}else{
			// 直接以配置指定目录为Skin根目录
			if (file.exists()&&file.isDirectory()){
				// 存在而且是文件夹，尝试构建主题类型对象
				themeSkin = new ThemeSkin(skinRoot);
				for (File themeTypeFile: file.listFiles()){
					if (themeTypeFile.isDirectory()){
						ThemeType themeType = ThemeType.getInstance(themeSkin, themeTypeFile, defaultThemeStyle);
						if (themeType!=null){
							themeSkin.themeTypeMap.put(themeTypeFile.getName().toLowerCase(),themeType);
							if (themeSkin.defaultType==null||themeTypeFile.getName().equalsIgnoreCase(defaultThemeType)){
								themeSkin.defaultType = themeTypeFile.getName().toLowerCase();
							}
						}
					}
				}
				log.info("检索到 "+themeSkin.themeTypeMap.size()+" 个主题类型，默认类型设置为 "+themeSkin.defaultType);
			}
		}
		
		return themeSkin;
	}

	public String getPath() {
		return skinRoot;
	}

	public void setPath(String path) {
		this.skinRoot = path;
	}
	
	/**
	 * 获取指定路径上的包含Skin对象
	 * @param path
	 * @return
	 */
	public static ThemeSkin getSkin(String path){
		ThemeSkin skin = null;
		
		if (path!=null){
			File file =new File(path);    
			if  (!file .exists() || !file .isDirectory())      
			{
			    log.debug("指定目录不存在，不是有效主题目录（"+path+"）");  
			} else {
				skin = new ThemeSkin(path);
				// 检查是否有至少一项有效的主题类型目录
				if (!skin.isValid()){
					skin = null;
					log.debug("指定目录下没有有效主题类型！");
				}
			}  
			
		}
		
		return skin;		
	}

	public boolean isValid() {
		boolean valid = false;
		
		if (themeTypeMap.size()>0){
			valid = true;
		}
		
		return valid;
	}

	public ThemeType getThemeType(String name) {
		ThemeType themeType = null;

		if (name!=null&&name.length()>0){
			String key = name.toLowerCase();
			if (themeTypeMap.containsKey(key)){
				themeType = themeTypeMap.get(name.toLowerCase());
			}
		}
		return themeType;
	}
	
	public ThemeType getDefault(){
		ThemeType defaultThemeType = null;
		
		if (themeTypeMap.size()>0){
			for (String themeTypeKey:themeTypeMap.keySet()){
				defaultThemeType = themeTypeMap.get(themeTypeKey);
			}
		}
		
		return defaultThemeType;
	}

	public List<ThemeType> getThemeTypeList(){
		List<ThemeType> list = new ArrayList<ThemeType>();
		
		for (String themeTypeName:themeTypeMap.keySet()){
			list.add(themeTypeMap.get(themeTypeName));
		}
		
		return list;
	}

	@Override
	public String toString() {
		return skinRoot;
	}
}
